#!/usr/bin/env python
# _*_ coding: utf-8 _*_
__author__ = 'yinq'

import sys

account_file = 'D:\wamp\S12\day1\match.txt'
locked_file = 'D:\wamp\S12\day1\locked.txt'


def deny_account(username):
    print("您的用户已被锁定！")
    with open(locked_file, 'a') as deny_f:
        deny_f.write('\n' + username)


def main():
    retry_count = 0
    retry_limit = 3

    while retry_count < retry_limit:
        username = input("\033[32:1m请输入用户名:\033[0m")

        with open(locked_file, 'r') as lock_f:
            # 采用with的文件打开方式，防止忘记f.close()关闭
            '''
            另外一种用户名匹配方式
            lines=[]
            for line in lock_f.readlines():
                lines.append(line.strip())
            if username in lines:
            '''
            for line in lock_f.readlines():
                if len(line) == 0:
                    continue
                if username == line.strip():
                    sys.exit("\033[32:1m用户 %s 已经被锁定！\033[0m" % username)
        if len(username) == 0:
            print("用户名不能为空，请重新输入")
            continue

        passward = input("\033:1m请输入密码:\033[0m")
        with open(account_file, 'a') as account_f:
            flag = False

            for line in account_f.readlines():
                user, pawd = line.strip().split()
                if username == user and passward == pawd:
                    print("success!")

                    flag = True
                    break

        if flag == False:
            if retry_count < 2:
                print("您的用户名或密码有误，请重新输入！")

            retry_count += 1

        else:
            print("欢迎用户%s来到老男孩登录系统" % username)
            break

    else:
        deny_account(username)


if __name__ == '__main__':
    main()
